<?php

class NGWordForm extends CFormModel
{
	public $content;
	
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return NGWordForm the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('content', 'safe'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'content' => 'Content',
		);
	}

	public function buildNGWordList() {
		$models = NGWord::model()->findAll();
		$res = '';
		foreach($models as $model) {
			$res .= "{$model->word}, {$model->replace_with}\n";
		}
		return $res;
	}
	
	public function saveNGWordList() {
		NGWord::model()->deleteAll();
		$rows = explode("\n", $this->content);
		foreach($rows as $row) {
			$row = trim($row);
			if(empty($row)) continue;
			
			$w = explode(',', $row);
			$model = new NGWord();
			$model->word=trim($w[0]);
			$model->replace_with=isset($w[1])? trim($w[1]) : '';
			$model->save();
		}
	}
}